package com.magicborrow.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;

import com.magicborrow.R;
import com.magicborrow.utils.UIHelper;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by wcx on 2015/9/17.
 */
public class SexDialog extends Dialog implements OnWheelChangedListener, View.OnClickListener {
    private Context mContext;
    private Button btnCancel;
    private Button btnConfirm;
    private WheelView wheelSex;

    private List<String> sexs=new ArrayList<>();

    public SexDialog(Context context) {
        super(context, R.style.selectYear_Dialog);
        this.mContext = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.dialog_year);
        initView();
        initData();
        initListener();
        setDialogSize(1d);
    }

    public void setDialogSize(double percent){
        Window window = this.getWindow();
        window.setGravity(Gravity.BOTTOM);
        window.setWindowAnimations(R.style.selectYear_Anim);  //添加动画
        int screenWidth = UIHelper.getScreenWidth(mContext);
        WindowManager.LayoutParams params = window.getAttributes();
        params.width=(int) (screenWidth*percent);
        params.height=params.WRAP_CONTENT;
        onWindowAttributesChanged(params);
        window.setAttributes(params);
    }

    private void initView() {
        btnCancel = (Button) findViewById(R.id.btn_cancel);
        btnConfirm = (Button) findViewById(R.id.btn_confirm);
        wheelSex = (WheelView) findViewById(R.id.wheelyear);
        setCanceledOnTouchOutside(true);
    }

    private void initListener() {
        wheelSex.addChangingListener(this);
        btnCancel.setOnClickListener(this);
        btnConfirm.setOnClickListener(this);
    }

    private TallButtonListener listener;
    public void setTallButtonListener(TallButtonListener listener) {
        this.listener = listener;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_confirm:
                listener.onPositiveButton(this);
                break;
            case R.id.btn_cancel:
                listener.onNegativeButton(this);
                break;
            default:
                break;
        }
    }

    public String getSex(){
        return sexs.get(wheelSex.getCurrentItem());
    }

    public interface TallButtonListener {
        void onPositiveButton(SexDialog dialog);
        void onNegativeButton(SexDialog dialog);
    }

    private void initData() {
        sexs.add("男");
        sexs.add("女");
        wheelSex.TEXT_SIZE=UIHelper.dip2px(mContext, 16);
        wheelSex.setCanSrcoll(sexs.size()>1?true:false);
        wheelSex.setAdapter(new SelectTallAdapter());
    }

    @Override
    public void onChanged(WheelView wheel, int oldValue, int newValue) {

    }
    class SelectTallAdapter implements WheelAdapter {
        @Override
        public int getItemsCount() {
            return sexs.size();
        }

        @Override
        public String getItem(int index) {
            return sexs.get(index);
        }

        @Override
        public int getMaximumLength() {
            return sexs.size() == 0 ? 1 : sexs.size();
        }
    }
}
